package com.hst.capacity.infra.util;

import cn.hutool.core.date.DatePattern;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

public class DateUtil {

    public static LocalDateTime parseLocalDateTime(String datetime) {
        try {
            if (datetime==null){
                return null;
            }
            if (datetime.length()>16){
                datetime=datetime.substring(0,16);
            }
            return cn.hutool.core.date.DateUtil.parseLocalDateTime(datetime, Constants.DATE_FORMAT);
        }catch (Exception e){
            throw new BusinessException(4003,"时间格式无法识别,请检查,参照["+Constants.DATE_FORMAT+"][2022-01-07 21:03]");
        }
    }

    public static LocalDateTime parseLocalDateTime(String datetime, String format) {
        try {
            return cn.hutool.core.date.DateUtil.parseLocalDateTime(datetime, format);
        }catch (Exception e){
            throw new BusinessException(4003,"时间格式无法识别,请检查,参照["+format+"][2022-01-07 21:03]");
        }
    }

    public static LocalDateTime parseLocalDayTime(String datetime) {
        try {
            return cn.hutool.core.date.DateUtil.parseLocalDateTime(datetime, DatePattern.NORM_DATE_PATTERN);
        }catch (Exception e){
            throw new BusinessException(4003,"时间格式无法识别,请检查,参照["+DatePattern.NORM_DATE_PATTERN+"][2022-01-07]");
        }
    }

    public static String formatDateTime(LocalDateTime datetime) {
        return cn.hutool.core.date.DateUtil.format(datetime, Constants.DATE_FORMAT);
    }

    public static String formatDayTime(LocalDateTime datetime) {
        return cn.hutool.core.date.DateUtil.format(datetime, DatePattern.NORM_DATE_PATTERN);
    }

    /**
     * 获取今日结束时间
     * @return
     */
    public static String getEndTimeOfDay() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 23, 59, 59);
        return cn.hutool.core.date.DateUtil.format(of,Constants.DATE_FORMAT);
    }

    /**
     * 获取今日开始时间
     * @return
     */
    public static String getStartTimeOfDay() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 00, 00, 00);
        return cn.hutool.core.date.DateUtil.format(of,Constants.DATE_FORMAT);
    }

    public static LocalDateTime getXHoursAgoDateTime(int xHourAgo) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime xHoursAgo = now.minusHours(xHourAgo);
        return xHoursAgo;

    }

    public static LocalDateTime getXHoursLaterDateTime(int xHourAgo) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime xHoursAgo = now.plusHours(xHourAgo);
        return xHoursAgo;

    }

    public static LocalDateTime getXMinutesAgoDateTime(long x) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime xMinutesAgo = now.minusMinutes(x);
        return xMinutesAgo;

    }

    /**
     * 获取今日开始时间
     * @return
     */
    public static String getNowTime() {
        Date date = cn.hutool.core.date.DateUtil.date();
        return cn.hutool.core.date.DateUtil.format(date, DatePattern.NORM_DATETIME_MINUTE_PATTERN);
    }

    /**
     * 获取今日开始时间
     * @return
     */
    public static String getNowString() {
        Date date = cn.hutool.core.date.DateUtil.date();
        return cn.hutool.core.date.DateUtil.format(date, DatePattern.PURE_DATETIME_FORMAT);
    }

    /**
     * 获取现在时间
     * @return
     */
    public static String getNowLocalDate() {
        LocalDateTime now = LocalDateTime.now();
        return cn.hutool.core.date.DateUtil.format(now, DatePattern.NORM_DATETIME_PATTERN);
    }

    /**
     * 获取昨天时间
     * @return
     */
    public static String getYestodayLocalDate() {
        LocalDateTime yestoday = LocalDateTime.now().minusDays(1L);
        return cn.hutool.core.date.DateUtil.format(yestoday, "yyyy-MM-dd 00:00:00");
    }

    /**
     * 获取今日结束时间
     * @return
     */
    public static LocalDateTime getEndTodayLocalDate() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime endToday = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 23, 59, 59);
        return endToday;
    }

    /**
     * 获取今日开始时间
     * @return
     */
    public static LocalDateTime getStartTodayLocalDate() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startToday = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 00, 00, 00);
        return startToday;
    }

    /**
     * 获取昨天时间
     * @return
     */
    public static String getThisMonthFirstDay() {
        LocalDate currentDate = LocalDate.now();
        LocalDate firstDayOfMonth = currentDate.withDayOfMonth(1);
        return firstDayOfMonth.toString();
    }

}
